package com.springmvc.xml;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.InputStream;

/**
 * @Author Lao Wang
 * @Date 2023/11/27
 * @Description 解析springmvc.xml
 **/
public class XmlParser {
    public static String getBasePackage(String xml){
        try {
            SAXReader saxReader = new SAXReader();
            InputStream inputStream = XmlParser.class.getClassLoader().getResourceAsStream(xml);
            //xml文档对象
            Document document = saxReader.read(inputStream);
            Element rootElement = document.getRootElement();
            Element componentScan = rootElement.element("component-scan");
            Attribute attribute = componentScan.attribute("base-package");
            return attribute.getText();
        } catch (DocumentException e) {
            throw new RuntimeException(e);
        }
    }
}
